//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.Windows.Controls;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Common;
using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.WorkflowManagerModule
{
    /// <summary>
    /// Interaction logic for DeletedWorkflowsListView.xaml
    /// </summary>
    public sealed partial class DeletedWorkflowsListView : UserControl
    {
        public DeletedWorkflowsListView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The event handler for the context menu opening event on the listview item.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            DeletedWorkflowsListPresenter listPresenter = this.DataContext as DeletedWorkflowsListPresenter;
            ListViewItem selectedItem = sender as ListViewItem;

            ContextMenu contextMenu = selectedItem.ContextMenu;
            object workflowModel = selectedItem.DataContext;

            foreach (MenuItem menuItem in listPresenter.MenuItemCollection)
            {
                ContextMenu parent = menuItem.Parent as ContextMenu;
                if (parent != null)
                {
                    parent.Items.Remove(menuItem);
                }

                menuItem.CommandParameter = workflowModel;
                contextMenu.Items.Add(menuItem);
            }

            if (workflowModel != null)
            {
                listPresenter.WorkflowSelectChangedCommand.Execute(workflowModel as WorkflowViewModel);
            }
        }

        /// <summary>
        /// Event handler for Display View Selection chaanged
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">SelectionChangedEventArgs</param>
        private void DisplayListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView item = sender as ListView;
            if (item != null)
            {
                WorkflowViewModel workflowModel = item.SelectedItem as WorkflowViewModel;

                if (workflowModel != null)
                {
                    (this.DataContext as DeletedWorkflowsListPresenter).WorkflowSelectChangedCommand.Execute(workflowModel);
                }
            }
        }
    }
}
